// pages/user/user/userWallet/userWallet.js
const app = getApp();
var util = require('../../../../utils/util.js');
import Api from '../../../../utils/http';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currMonth: '本月',
    chooseMonth: '', //选中的月份
    endMonth: util.formatMonth(new Date), //限制不可选取未来的月份
    dataList: [],
    balance: null,
    page: 1, //页面页码
    pageSize: 9, //每页显示条数
  },
  //选择月份
  bindMonthChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var chooseMonth = e.detail.value;
    if (chooseMonth == util.formatMonth(new Date)) {
      chooseMonth = '本月'
    }
    this.setData({
      currMonth: chooseMonth
    })
    this.getBalanceScreen(e);
  },
  //跳转余额提现
  toWithdrawal: function () {
    wx.showToast({
      title: '此功能暂未开通，尽请期待',
      icon:'none'
    })
    // wx.navigateTo({
    //   url: '/pages/user/user/userWithdrawal/userWithdrawal',
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      chooseMonth: util.formatMonth(new Date)
    });
    that.getBalanceList();

    // const eventChannel = this.getOpenerEventChannel()
    // eventChannel.emit('acceptDataFromOpenedPage', { data: 'test' })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo();
  },
  //筛选获取收益列表
  getBalanceScreen: function (e) {
    var that = this
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      time:e.detail.value,
      pageNum: 1,
      pageSize: 10
    }
    console.log(param);
    Api.ajax('/app/Zindex/water', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          dataList: res.data,
        });
      } else {
        wx.showToast({
          title: '暂无数据',
          icon:'none',
          duration:1000,
        })
        that.setData({
          dataList: 0
        })
      }
    })
  },
  //头部我的账户余额
  getUserInfo: function () {
    var that = this
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    }
    console.log(param);
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          balance: res.data.balance,
        });
      }
    })
  },
  //获取收益列表
  getBalanceList: function () {
    var that = this
    let uid = wx.getStorageSync('userid');
    var page = that.data.page;
    var datalist = that.data.dataList;
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      pageNum: page,
      pageSize: 10
    }
    console.log(param);
    Api.ajax('/app/Zindex/water', param).then(res => {
      console.log(res)
      if (res.code == 200) {
       
        Page++;
        that.setData({
          dataList: datalist.concat(res.data),
          page: Page,
          // balance: res.data.balance,
          // dataTotal
        });
      } else {
        that.setData({
          dataList: 0
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh();
  },
  //下拉刷新
  refreshData: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      pageNum: 1,
      pageSize: that.data.pageSize,
    }
    console.log(param)
    Api.ajax("/app/Zmanage/water", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          dataList: res.data,
          page: 2,
        });
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getBalanceList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})